#ifndef SVGICONENGINE_H
#define SVGICONENGINE_H

#include <QIconEngine>
#include <QPainter>
#include <QPixmap>
#include <QRect>
#include <QtSvg/QSvgRenderer>

class SvgIconEngine:public QIconEngine
{
    Q_OBJECT
private:
    QSvgRenderer* m_render;
public:
    SvgIconEngine(QString& svgFile):m_render(new QSvgRenderer(svgFile)){

    }
    ~SvgIconEngine(){
        delete m_render;
    }
    void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
    {
        painter->setRenderHint(QPainter::Antialiasing);
        m_render->render(paint,rect);
    }
    // 重写 pixmap 方法，确保 QIcon::pixmap() 调用时也能正确返回
    QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override {
        QPixmap pix(size);
        pix.fill(Qt::transparent);
        QPainter p(&pix);
        paint(&p, QRect(QPoint(0, 0), size), mode, state);
        return pix;
    }

};

#endif
